package com.cn.chaomeng.service.impl;

import com.cn.chaomeng.constant.SysParam;
import com.cn.chaomeng.dao.FoodMapper;
import com.cn.chaomeng.dao.OrderfoMapper;
import com.cn.chaomeng.dao.OrdersMapper;
import com.cn.chaomeng.dao.UserMapper;
import com.cn.chaomeng.model.Orderfo;
import com.cn.chaomeng.model.Orders;
import com.cn.chaomeng.model.example.OrderfoExample;
import com.cn.chaomeng.model.example.OrdersExample;
import com.cn.chaomeng.model.example.UserExample;
import com.cn.chaomeng.service.IOrdersService;
import com.cn.chaomeng.service.IUserService;
import com.cn.chaomeng.vo.BootstrapTableVo;
import com.cn.chaomeng.vo.OrderfoVo;
import com.cn.chaomeng.vo.table.OrdersTable;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LYJ
 *         Created by Administrator on 2018/3/7 0007.
 */
@Service("ordersService")
public class OrdersServiceImpl implements IOrdersService {

    @Resource
    private FoodMapper foodMapper;

    @Resource
    private IUserService userService;

    @Resource
    private OrderfoMapper orderfoMapper;

    @Resource
    private OrdersMapper ordersMapper;

    @Override
    public BootstrapTableVo getOrdersTable(Integer offset, Integer limit) {
        BootstrapTableVo vo = new BootstrapTableVo();

        List<OrdersTable> ordersTables = new ArrayList<OrdersTable>();
        OrdersExample example = new OrdersExample();


        List<String> stateStringList = new ArrayList<String>();
        stateStringList.add(SysParam.SUCCESS_PAY_STATE);
        stateStringList.add(SysParam.FAIL_PAY_STATE);
        OrdersExample.Criteria criteria =  example.createCriteria();
        criteria.andStateIn(stateStringList);

//        vo.setTotal((int)ordersMapper.countByExample(new OrdersExample()));
        vo.setTotal((int)ordersMapper.countByExample(example));

        example.setOrderByClause("orderid desc");

        for (Orders orders : ordersMapper.selectByExampleWithRowbounds(example,new RowBounds(offset,limit))){
            ordersTables.add(new OrdersTable(orders,userService.selectByOpenId(orders.getOpenid())));
        }
        vo.setRows(ordersTables);

        return vo;
    }

    @Override
    public List<OrderfoVo> selectOrderfoVoByOrderId(Integer orderId) {
        OrderfoExample example = new OrderfoExample();
        example.createCriteria().andOrderidEqualTo(orderId);
        List<OrderfoVo> orderfoVos = new ArrayList<OrderfoVo>();
        for (Orderfo orderfo : orderfoMapper.selectByExample(example)){
            orderfoVos.add(new OrderfoVo(orderfo,foodMapper.selectByPrimaryKey(orderfo.getFid())));
        }
        return orderfoVos;
    }


}
